<?php

$dsn = 'mysql:dbname=weather;host=sajb.co.uk';
$user = 'weatheruser';
$password = 'dragon32';
try {
//      error_log("Trying to Connect");
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
    die("Died in Connection");
}

$sql = "SELECT DATE_FORMAT(Date, '%d/%m/%Y') AS Date, AVG(`Temp`) AS Temp, AVG(`WindSpeed`) AS WindSpeed, AVG(`Pressure`) AS Pressure, AVG(`Humidity`) AS Humidity, AVG(`FeelsLike`) AS FeelsLike FROM `RoundedLast7Days`
GROUP BY DAYOFYEAR(Date)";
try {
    $sth = $dbh->prepare($sql);
} catch (PDOException $e) {
    echo 'Prepare failed: ' . $e->getMessage();
    die("Died in Prepare");
}

try {
    $sth->execute();
} catch (PDOException $e) {
    echo 'Execution failed: ' . $e->getMessage();
    die("Died in execute");
}

$tableData = $sth->fetchAll(PDO::FETCH_ASSOC);
$jsonData = json_encode($tableData, JSON_NUMERIC_CHECK);

print $jsonData;
/*
[
  {
      "Date": "04/27/2013 15:14",
      "Time": " 15:14 BST ",
      "TempOut": 11.2,
      "FeelsLike": 8.8,
      "HumidityOut": 53,
      "WindDirection": " NE ",
      "WindAvg": 1,
      "WindGust": 2,
      "Rain": 0.0,
      "AbsPressure": 1010.0  },
 ]
*/ 
